//
//  RostViewController.m
//  Xmpp
//
//  Created by lanou3g on 16/1/29.
//  Copyright © 2016年 徐强. All rights reserved.
//

#import "RostViewController.h"
#import "Manager.h"
#import "ChatViewController.h"
@interface RostViewController ()<XMPPRosterDelegate,UITableViewDataSource,UITableViewDelegate,XMPPStreamDelegate>

@property (nonatomic , strong)NSMutableArray *rostArray;
@property (nonatomic , strong)UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *textUser;



@end

@implementation RostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //XMPPStream添加代理
    [[Manager shareManage].stream addDelegate:self delegateQueue:dispatch_get_main_queue()];
    [[Manager shareManage].rost addDelegate:self delegateQueue:dispatch_get_main_queue()];
    //初始化数组
    self.rostArray = [NSMutableArray array];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, 375, 567) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}
//发送自己的状态变更
- (void)xmppStream:(XMPPStream *)sender didSendPresence:(XMPPPresence *)presence
{
    NSLog(@"自己状态：%@",presence.type);

}
//接收好友的状态变更
- (void)xmppStream:(XMPPStream *)sender didReceivePresence:(XMPPPresence *)presence{
    NSLog(@"%@状态：%@",presence.from.user,presence.type);
}

//接收好友请求
- (void)xmppRoster:(XMPPRoster *)sender didReceivePresenceSubscriptionRequest:(XMPPPresence *)presence{
    NSLog(@"接收到好友请求");
}

//开始获取好友列表
- (void)xmppRosterDidBeginPopulating:(XMPPRoster *)sender{
    NSLog(@"获取好友列表");
}
//结束获取好友列表
- (void)xmppRosterDidEndPopulating:(XMPPRoster *)sender{
    NSLog(@"结束获取好友列表");
}
//接收到一个好友调用的方法
-(void)xmppRoster:(XMPPRoster *)sender didReceiveRosterItem:(DDXMLElement *)item{
    //好友的几种状态
    //both 相互添加
    //to   添加别人，别人没同意
    //from 别人添加你，你没同意
    //none 两个人没关系
    //remove 你删除了好友
    NSString *subscriotion = [[item attributeForName:@"subscription"] stringValue];
    if ([subscriotion isEqualToString:@"both"]) {
        //获取到好友的id
        NSString *pjid = [[item attributeForName:@"jid"] stringValue];
        NSLog(@"========%@========",pjid);
        XMPPJID *ppjid = [XMPPJID jidWithString:pjid];
        //如果不包含这个ppjid，添加进去
        if(![self.rostArray containsObject:ppjid]){
            
            [self.rostArray addObject:ppjid];
            //防止数组越界
            if(self.rostArray.count < 1){
                return;
            }
            //tableView中插入一行数据
            NSIndexPath *path = [NSIndexPath indexPathForRow:self.rostArray.count-1 inSection:0];//插入位置
            [self.tableView insertRowsAtIndexPaths:@[path] withRowAnimation:UITableViewRowAnimationLeft];
        }
        
    }
}
#pragma mark -添加好友的方法
- (IBAction)addAction:(id)sender {
    
    if (self.textUser.text == nil) {
        return;
    }
    XMPPJID *jid = [XMPPJID jidWithUser:self.textUser.text domain:@"lanou3gdemac-mini.local" resource:@"iOS"];
    [[Manager shareManage] addFriendWith:jid];
    [self.textUser endEditing:YES];
    
}
#pragma mark - 注销
- (IBAction)loginOut:(id)sender {
    [[Manager shareManage] loginOut];
#warning 这是两个storyBoard的转换，并不是模态过来的，所以不能模态返回
//    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.rostArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *string = @"cell_id";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    }
    XMPPJID *jidc = self.rostArray [indexPath.row];
    cell.textLabel.text = jidc.user;

    return cell;
}
#pragma mark - 删除好友
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除好友
        XMPPJID *jid = self.rostArray[indexPath.row];
        //删除数据库
        [[Manager shareManage] removeFriendWith:jid];
        //列表数组中
        [self.rostArray removeObjectAtIndex:indexPath.row];
        //删除列表
        [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationAutomatic)];
        NSLog(@"删除%@成功",jid.user);
    }
}


//进入聊天界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"RostStoryBoard" bundle:nil];
    
    ChatViewController *chatVC = [story instantiateViewControllerWithIdentifier:@"ChatViewController"];
    //把联系人的jid传递过去
    chatVC.jid = self.rostArray[indexPath.row];
    [self presentViewController:chatVC animated:YES completion:nil];
}


@end
